package com.pengtu.gsj.service.impl.check.lzgl.roadProtect;

import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.lzgl.RoadSupInNoteDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.lzgl.RoadSupInNote;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.lzgl.roadProtect.RoadSupInspectService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.DateUtils;
import com.pengtu.gsj.utils.MathsUtils;

@Service
@Transactional
public class RoadSupInspectServiceImpl implements RoadSupInspectService {

	@Autowired
	public SystemManageService systemManageService;
	@Autowired
	public CheckTableService checkTableService;
	@Autowired
	public RoadSupInNoteDao roadSupInNoteDao;
	
	@Override
	public Map<String, Object> getRoadSupInspects(String startTime, String endTime, CheckTable checkTable, String tag) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Attachment> list = new ArrayList<Attachment>();
		List<Attachment> attResults = new ArrayList<Attachment>();
		List<Double> scoLists = new ArrayList<Double>(); 
		List<RoadSupInNote> roadSupInNotes = new ArrayList<RoadSupInNote>();
		RoadSupInNote roadSupInNote;
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		String itemIds = (String)jsonObject.get("itemProjectId");
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		double poitMark = Double.parseDouble((String)jsonObject.get("poitMark"));
		double tableFull = MathsUtils.sumDouble((yearFull + poitMark) * 2, yearPart);
		double percentage = 0; //完善率
		double scoreOne = 0; // 计算表一总分
		double total = 0; //计算总分
		boolean flag = false;
		
		//根据时间、标签查询是否有附件
		list = systemManageService.getCheckAttachments(startTime, endTime, tag);
		map.put("att", list);
		if (StringUtils.isNotEmpty(itemIds)) {
			map.put("flagOne", true);
			//通过逗号将每个id切割出来，并搜索算分
			String[] idArrs = itemIds.split(",");
			for (int i = 0; i < idArrs.length; i++) {
				if( i > 1) {
					break;
				}
			roadSupInNote = roadSupInNoteDao.get(idArrs[i]);
			roadSupInNotes.add(roadSupInNote);	
			//1、如果国检或省道检查平均每周的检查次数大于等于1次的话，说明检查是符合要求的，得1/2的yearFull分数
			scoreOne = roadSupInNote.getCouCheck()/52 >= 1 || roadSupInNote.getProCheck()/52 >= 1 ? yearFull/2 : 0;
			//2、用remark属性保存是否检查记录附件，有说明记录详实，得1/2的yearFull的分数
			for (Attachment att : roadSupInNote.getAttachments()){
				if (att.getTag().equals("gljdxc_xcjl")) {
					if (!"1".equals(roadSupInNote.getRemark())) {
						scoreOne += yearFull/2;
					}
					roadSupInNote.setRemark("1");
				}
			}
			//3、查询是否处理得当
			if (null != roadSupInNote.getIfInspProc()) {
				if ("1".equals(roadSupInNote.getIfInspProc())) {
					scoreOne += yearFull/2;
				}
			}
			scoreOne = scoreOne > yearFull ? yearFull : scoreOne > yearFull/2 ? yearFull/2 : 0;
			//默认开展联合路政养护
			scoreOne += poitMark;
			scoLists.add(scoreOne);
			total += scoreOne;
			}
		}
		
		//4、是否建立路政与养护联合巡查协作机制
		list = systemManageService.getAllCheckAttachments("gljdxc_gzzd");
		for (Attachment attachment : list) {
			if (0 <= attachment.getFileName().indexOf("联合巡查")) {
				attResults.add(attachment);
				map.put("flagTwo", true);
				if (!flag) {
					total += yearPart;
					map.put("yearPart", yearPart);
					flag = true;
				}
			}
		}
		percentage = MathsUtils.divDouble(total*100, tableFull, 2);
		
		//只有当系统完善度和检查得分不相等的情况下才更新数据  避免每次搜索都更新数据
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, true);
		map.put("scoLists", scoLists);
		map.put("percent", percentage);
		map.put("yearPart", yearPart);
		map.put("attResults", attResults);
		map.put("roadSupInNotes", roadSupInNotes);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		return map;
	}
	
	/**
	 * 将检索条件封装，进行RoadSupInNote的文件查询
	 */
	@Override
	public List<RoadSupInNote> getRoadSupInNotes(String startTime, String endTime) {
		QueryFilter queryFilter = new QueryFilter();
		if (StringUtils.isNotEmpty(startTime) && DateUtils.isDate(startTime)) {
			queryFilter.addFilter("year", startTime, MatchType.GE);
		}
		if (StringUtils.isNotEmpty(endTime) && DateUtils.isDate(endTime)) {
			queryFilter.addFilter("year",  endTime, MatchType.LE);
		}
		return roadSupInNoteDao.find(queryFilter);
	}
}
